import tkinter as tk
from tkinter import messagebox
import os

def create_window():
    # 创建主窗口
    root = tk.Tk()
    root.title("flow 测试")
    
    # 设置窗口大小
    root.geometry("600x600")
    
    # 设置窗口背景颜色
    root.configure(bg="#f0f0f0")
    
    return root

def add_questions(root):
    questions = [
        ("评价一下今天的满意程度", ['4','3','2','1']),
        ("今天有破戒次数吗", ["没有", "一次", "两次"])
    ]
    
    blank_questions = [
        "答题时间：",
        "今日工作时长：",
        "今日学习时长：",
        "早上起床时间：",
        "评价一下今日的工作状态：",
        "评价一下今日的生活状态：",
        "评价一下一天三餐和花销：",
        "今日学习的知识点有：",
        "今天觉得不快乐的地方："
    ]
    
    # 文件名输入
    filename_question = "请输入保存文件的名字（不包括扩展名）："
    
    answers = []
    all_questions = []  # 保存所有问题，包括选择题和填空题
    
    # 创建一个Canvas和Scrollbar
    canvas = tk.Canvas(root, bg="#f0f0f0")
    scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
    scrollable_frame = tk.Frame(canvas, bg="#f0f0f0")
    
    scrollable_frame.bind(
        "<Configure>",
        lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
    )
    
    canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
    canvas.configure(yscrollcommand=scrollbar.set)
    
    # 添加填空题 - 答题时间
    blank_frame = tk.Frame(scrollable_frame, bg="#f0f0f0")
    blank_frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
    
    blank_label = tk.Label(blank_frame, text=blank_questions[0], bg="#f0f0f0", fg="#333333", font=("Arial", 10))
    blank_label.pack(anchor=tk.W, pady=(0, 5))
    
    blank_entry = tk.Entry(blank_frame, font=("Arial", 10))
    blank_entry.pack(anchor=tk.W, padx=10, pady=5)
    
    answers.append(blank_entry)
    all_questions.append(blank_questions[0])
    
    # 添加选择题
    for i, (question, options) in enumerate(questions):
        # 创建一个LabelFrame来容纳问题和选项
        frame = tk.LabelFrame(scrollable_frame, text=question, bg="#f0f0f0", fg="#333333", font=("Arial", 12, "bold"))
        frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
        
        # 创建选项
        var = tk.StringVar()
        for option in options:
            rb = tk.Radiobutton(frame, text=option, variable=var, value=option, bg="#f0f0f0", fg="#333333", font=("Arial", 10))
            rb.pack(anchor=tk.W, padx=10, pady=5)
        
        # 将问题和变量添加到列表中，以便后续获取答案
        answers.append(var)
        all_questions.append(question)
    
    # 添加剩余的填空题
    for i, question in enumerate(blank_questions[1:]):
        blank_frame = tk.Frame(scrollable_frame, bg="#f0f0f0")
        blank_frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
        
        blank_label = tk.Label(blank_frame, text=question, bg="#f0f0f0", fg="#333333", font=("Arial", 10))
        blank_label.pack(anchor=tk.W, pady=(0, 5))
        
        blank_entry = tk.Entry(blank_frame, font=("Arial", 10))
        blank_entry.pack(anchor=tk.W, padx=10, pady=5)
        
        answers.append(blank_entry)
        all_questions.append(question)
    
    # 添加文件名输入
    filename_frame = tk.Frame(scrollable_frame, bg="#f0f0f0")
    filename_frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
    
    filename_label = tk.Label(filename_frame, text=filename_question, bg="#f0f0f0", fg="#333333", font=("Arial", 10))
    filename_label.pack(anchor=tk.W, pady=(0, 5))
    
    filename_entry = tk.Entry(filename_frame, font=("Arial", 10))
    filename_entry.pack(anchor=tk.W, padx=10, pady=5)
    
    answers.append(filename_entry)
    
    canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    
    return answers, all_questions

def get_custom_save_path():
    # 自定义保存路径，例如 D:\MyAnswers
    custom_path = "D:\\desktop\\windows\\flow"
    
    # 确保目录存在，如果不存在则创建
    if not os.path.exists(custom_path):
        os.makedirs(custom_path)
    
    return custom_path

def save_answers(answers, all_questions):
    custom_path = get_custom_save_path()
    filename = answers[-1].get()  # 获取用户输入的文件名
    if not filename:
        messagebox.showerror("错误", "文件名不能为空")
        return
    
    file_path = os.path.join(custom_path, f"{filename}.txt")
    
    with open(file_path, "w", encoding="utf-8") as file:
        for i, answer in enumerate(answers[:-1]):  # 不包括最后一个文件名输入
            question = all_questions[i]
            file.write(f"{question} {answer.get()}\n")
    
    messagebox.showinfo("保存成功", f"答案已保存到 {file_path}")

def create_save_button(root, answers, all_questions):
    save_button = tk.Button(root, text="保存答案", command=lambda: save_answers(answers, all_questions), bg="#4CAF50", fg="white", font=("Arial", 12, "bold"), relief=tk.RAISED)
    save_button.pack(pady=20)

def main():
    root = create_window()
    answers, all_questions = add_questions(root)
    create_save_button(root, answers, all_questions)
    root.mainloop()

if __name__ == "__main__":
    main()